<template>
  <div>
    <Header></Header>
    <section>
      <Routers></Routers>
      <div id="section" class="main">
        <div class="common_header">
          <span class="common_header_tip">企业所得税纳税申报表</span>
          <div>
            <button class="common_header_btn" @click="add">添加</button>
            <button class="common_header_btn" @click="batchDel">批量删除</button>
          </div>
        </div>

        <table class="common_table" cellspacing="0" cellpadding="0">
          <thead>
          <tr>
            <th><input type="checkbox" v-model='checked' @click='checkedAll'></th>
            <th>年份</th>
            <th>企业纳税申报表</th>
            <th>操作</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="(item ,index) in list" :key="index">
            <td><input type="checkbox" v-model='checkList' :value='item.id'></td>
            <td><span>{{item.year}}</span></td>
            <td><a :href="item.account_report" target="_blank" class="common_showpdf_text">{{item.account_report_info}}</a></td>
            <td class="operation_td">
              <span class="common_table_editBTN" @click="edit(item.id)">编辑</span>
              <span class="common_table_delBTN" @click="del(item.id)">删除</span>
            </td>
          </tr>
          </tbody>
        </table>

        <!--分页-->
        <ul class="pagination" >
          <li v-show="current != 1" @click="current-- && goto(current)" ><a href="#">上一页</a></li>
          <li v-for="index in pages" @click="goto(index)" :class="{'active':current == index}" :key="index">
            <a href="#" >{{index}}</a>
          </li>
          <li v-show="allpage != current && allpage != 0 " @click="current++ && goto(current++)"><a href="#" >下一页</a></li>
        </ul>
        <!--文字弹框 -->
        <div class="tankuang_box" v-if="tankuangISshow">
          <div class="tankuang">
            <p class="tankuangMSG">{{tankuangMSG}}</p>
            <button class="tankuang_ok_btn" @click="tankuangISshowBTN">确认</button>
          </div>
        </div>

        <!--添加的弹框-->
        <div class="tankuang_box" v-if="boxISshow">
          <!--里面的白色盒子（需要自己设置宽高）-->
          <div class="add_tankuang">
            <!--顶部的文字和叉叉（通用）-->
            <div class="add_tankuang_header">
              <span>{{tankuang_title}}</span>
              <i class="iconfont icon-guanbi" @click="closeBox"></i>
            </div>
            <div class="add_tankuang_content">
              <div class="add_tankuang_content_box">
                <!--这里的内容是根据需要的内容加入（不通用）-->
                <ul class="common_tankuang_content">
                  <li>
                    <span>选择年份：</span>
                    <select class="common_header_select" v-model="year">
                      <option :value="item" v-for="(item, index) in year_list">{{item}}</option>
                    </select>
                  </li>
                  <li class="fileList">
                    <span>企业所得税纳税申报表：</span>
                    <div class="file_box">
                      <div class="filebox">
                        <i class="inlineBlockIcon upload_logo"></i>
                        <span class="upload_text">上传文件</span>
                        <input type="file" @change="upload($event)">
                      </div>
                      <div class="file_content_box" v-if="file1_upload_ed">
                        <p class="file_size_err" v-if="file1_size_err">文件太大了！</p>
                        <p class="file_content_text">
                          <i class="inlineBlockIcon file_logo"></i>
                          <span class="file_text" :class="{'active':file1_upload_ing}">{{file1_name}}</span>
                        </p>
                        <div class="progress" v-if="file1_upload_ing"><div :style="'width:'+file1_w+'px'"></div></div>
                      </div>
                    </div>
                  </li>
                  <li>
                    <p class="tips">注：需上传的文件，小于10M，PDF格式</p>
                  </li>
                </ul>
                <!--底部的按钮（通用）-->
                <div class="add_tankuang_btnBOX">
                  <button class="add_tankuang_btn add_tankuang_btn_first" @click="save">保存</button>
                  <button class="add_tankuang_btn" @click="cancelSave">取消</button>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!--删除确认框 -->
        <div class="tankuang_box" v-if="delISshow">
          <div class="tankuang">
            <p class="tankuangMSG">确认删除吗？</p>
            <div class="tankuang_btn_box">
              <button class="tankuang_ok_btn" @click="delBTN">确认</button>
              <button class="tankuang_ok_btn" @click="cancelDel">取消</button>
            </div>
          </div>
        </div>

      </div>
    </section>
  </div>
</template>

<script>
  import Routers from '@/template/routers.vue'
  import Header from '@/template/common_header.vue'
  import { Toast } from 'mint-ui';
  export default {
    name: 'TaxReports',
    components: {
      Routers,
      Header
    },
    data () {
      return {
        id:'',
        list:[],
        year_list:[],

        year:'',
        file1:null,
        file1_name:'',
        file1_upload_ed:false,
        file1_upload_ing:false,
        file1_size_err:false,
        file1_progressBar:null,
        file1_w:0,

        /*控制*/
        boxISshow:false,
        delISshow:false,
        tankuangISshow:false,
        /****全选单选反选****/
        checked:false,
        checkList:[],
        /***删除***/
        del_list:[],
        /****分页****/
        current:1,
        showItem:5,
        allpage:1
        /***********/
      }
    },
    computed:{
      pages:function(){
        let pag = [];
        if( this.current < this.showItem ){ //如果当前的激活的项 小于要显示的条数
          //总页数和要显示的条数那个大就显示多少条
          let i = Math.min(this.showItem,this.allpage);
          while(i){
            pag.unshift(i--);
          }
        }else{ //当前页数大于显示页数了
          let middle = this.current - Math.floor(this.showItem / 2 ),//从哪里开始
            i = this.showItem;
          if( middle >  (this.allpage - this.showItem)  ){
            middle = (this.allpage - this.showItem) + 1
          }
          while(i--){
            pag.push( middle++ );
          }
        }
        return pag
      }
    },
    watch: {//深度 watcher
      'checkList': {
        handler: function(val, oldVal) {
          if (val.length === this.list.length) {
            this.checked = true;
          } else {
            this.checked = false;
          }
        },
        deep: true
      }
    },
    mounted: function() {
      this.axios.get('/index/taxes_list?page=1').then((res)=>{
        if(res.data.code==1){
          this.list = res.data.data
          this.allpage = res.data.last_page
        }else{
          this.list = []
        }
      })
      let year = new Date().getFullYear()
      for(let i = year; i>=year-10; i--){
        this.year_list.push(i)
      }
    },
    methods: {
      /*上传文件*/
      upload (e) {
        let file = e.target.files[0]
        let readers = new FileReader()
        readers.readAsDataURL(file)
        let _this = this
        _this.file1_size_err = false
        if(file.size > 10*1024*1024){
          Toast('请上传小于10M的文件！')
          _this.file1_size_err = true
          return false
        }
        _this.file1_name = file.name
        _this.file1_upload_ed = true
        readers.onload = function(e) {
          _this.file1_upload_ing = true
          _this.axios.post('/index/upload_qiniu',{
            code:this.result
          }).then((res)=>{
            if(res.data.code==1){
              _this.file1_w = 200
              clearTimeout(_this.file1_progressBar)
              _this.file1 = res.data.key
              _this.file1_upload_ing = false
              _this.file1_size_err = false
              _this.file1_w = 0
            }else{
              Toast(res.data.msg)
              return
            }
          }).catch(function (error) {
            Toast('网络连接超时，请稍后重试！')
            return
          });
          let n = _this.file1_w
          _this.file1_progressBar = setInterval(function(){
            n+=5
            _this.file1_w = n
            if(n>=65){
              clearTimeout(_this.file1_progressBar)
            }
          },150)
        }
      },
      /*分页*/
      goto (index) {
        this.current = index;
        //这里可以发送ajax请求
        this.axios.get('/index/taxes_list?page='+this.current).then((res)=>{
          if(res.data.code==1){
            this.list = res.data.data
            this.allpage = res.data.last_page
          }else{
            this.list = []
          }
        })
      },
      /***全选*****/
      checkedAll () {
        let _this = this;
        if (!this.checked) {//实现全选
          _this.checkList = [];
          _this.list.forEach(function(item) {
            _this.checkList.push(item.id);
          });
        }else{//实现反选
          _this.checkList = [];
        }
      },
      /*删除*/
      del (id) {
        this.del_list = []
        this.del_list.push(id)
        this.delISshow = !this.delISshow
      },
      batchDel () {
        if(this.checkList.length<=0){
          this.tankuangMSG = '没有选择任何内容'
          this.tankuangISshow = !this.tankuangISshow
        }else{
          this.delISshow = !this.delISshow
        }
      },
      delBTN () {
        if(this.del_list.length>0){
          this.axios.post('/index/del_taxes',{
            ids:JSON.stringify(this.del_list)
          }).then((res)=>{
            if(res.data.code==1){
              this.delISshow = !this.delISshow
              this.del_list = []
              this.axios.get('/index/taxes_list?page='+this.current).then((res)=>{
                if(res.data.code==1){
                  this.list = res.data.data
                  this.allpage = res.data.last_page
                }else{
                  this.list = []
                }
              })
            }else{
              Toast(res.data.msg)
              return
            }
          })
        }else{
          this.axios.post('/index/del_taxes',{
            ids:JSON.stringify(this.checkList)
          }).then((res)=>{
            if(res.data.code==1){
              this.delISshow = !this.delISshow
              this.checkList = []
              this.checked = false
              this.axios.get('/index/taxes_list?page='+this.current).then((res)=>{
                if(res.data.code==1){
                  this.list = res.data.data
                  this.allpage = res.data.last_page
                }else{
                  this.list = []
                }
              })
            }else{
              Toast(res.data.msg)
              return
            }
          })
        }
      },
      /*基础操作*/
      add () {
        this.boxISshow = true
        this.tankuang_title = '添加'
      },
      edit (id) {
        this.tankuang_title = '编辑'
        this.axios.get('/index/show_taxes?id='+id).then((res)=>{
          if(res.data.code==1){
            let datas = res.data.data
            this.year = datas.year
            if(datas.account_report){
              this.file1 = datas.account_report
              this.file1_name = datas.account_report_info
            }
            this.id = id
            this.boxISshow = true
          }else{
            Toast(res.data.msg)
            return
          }
        })
      },
      save () {
        this.axios.post('/index/save_taxes',{
          id:this.id||'',
          year:this.year,
          account_report:this.file1,
          account_report_info:this.file1_name,
        }).then((res)=>{
          if(res.data.code==1){
            this.axios.get('/index/taxes_list?page=1').then((res)=>{
              if(res.data.code==1){
                this.list = res.data.data
                this.allpage = res.data.last_page
              }else{
                this.list = []
              }
            })
            this.boxISshow = false
            this.year = ''
            this.file1 = ''
            this.file1_name = ''
            this.id = null
          }else{
            Toast(res.data.msg)
            return
          }
        })
      },
      cancelSave () {
        this.boxISshow = !this.boxISshow
        this.year = ''
        this.file1 = ''
        this.file1_name = ''
        this.id = null
      },
      closeBox () {
        this.boxISshow = !this.boxISshow
        this.year = ''
        this.file1 = ''
        this.file1_name = ''
        this.id = null
      },
      cancelDel () {
        this.delISshow = !this.delISshow
      },
      tankuangISshowBTN(){
        this.tankuangISshow = false
      }
    }
  }
</script>

<style scoped>
  @import '../../assets/public.css';
  @import '//at.alicdn.com/t/font_749860_je5eki3tevb.css';

  .add_tankuang{
    width:550px;
    height:380px;
  }
  .common_tankuang_content{
    padding:20px 0;
  }
  .common_tankuang_content>li{
    width:100%;
  }
  .common_tankuang_content>li>span{
    width: 200px !important;
    text-align: right;
  }
  .common_header_select{
    margin-left:0;
  }
  .tankuang{
    width:300px;
    height:157px;
    padding:30px;
  }
  .tankuang_ok_btn{
    padding:6px 15px;
    font-size:16px;
    width:110px;
  }

  .fileList{
    display: flex;
    align-items: flex-start!important;
  }
  .fileList>span{
    padding-top:7px;
  }
  .filebox{
    width:200px;
    height:32px;
    border: 1px solid #ddd;
    display: flex;
    align-items: center;
    justify-content:center;
    position: relative;
    box-sizing: border-box;
  }
  i.upload_logo{
    width:22px;
    height:22px;
    background: url("../../assets/img/upload.png");
    margin-right:5px;
  }
  span.upload_text{
    color: #888;
    font-size:16px;
  }
  .filebox>input{
    width:100%;
    height:100%;
    position: absolute;
    top:0;
    left:0;
    right: 0;
    bottom: 0;
    opacity: 0;
    cursor: pointer;
  }
  .file_content_box{
    margin-top:5px;
  }
  .file_content_text{
    display: flex;
    align-items: center;
  }
  i.file_logo{
    width:19px;
    height:17px;
    background: url("../../assets/img/file.png");
    margin-right:5px;
  }
  span.file_text{
    color: #333;
    font-size: 12px;
  }
  span.file_text.active{
    color: #888;
  }

  .file_size_err{
    color: #ff0000;
    font-size: 14px;
  }

  .progress{
    width:220px;
    height:3px;
    background: #f3f3f3;
    border-radius: 2px;
    position: relative;
    margin-top:10px;
  }
  .progress>div{
    position: absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;
    width:0;
    height:3px;
    background: #4797D4;
  }

  p.tips{
    width:100%;
    color: #888;
    font-size:14px;
    text-align: center;
    margin:10px 0;
  }


</style>
